/*******************************************************************************
 *
 *	:  IO
 *
 *******************************************************************************/
#define PORT_PIN_CTEAT
#include "bsp.h"
#include "bsp_io.h"
#include "iohandle.h"
#include "pincfg.h"
#include "mytype.h"

/*******************************************************************************
 * @brief  ext sim io
 *******************************************************************************/
uint8_t ext_port_out[4][8];
uint8_t ext_port_in[4];


/*******************************************************************************
 * @brief  gpio cfg list
 *******************************************************************************/
static gpio_init_cfg_type gpio_cfg[] = {
0
};

/*******************************************************************************
 * @brief  bsp_gpio_init
 * \param[in] us delay
 * \retval: none
 *******************************************************************************/
void bsp_gpio_init(void)
{
	int index;
	
	o_led1.mode(PIN_MODE_OUT_PP);
}


/*******************************************************************************
 * @brief  bsp_gpio
 * \param[in] us delay
 * \retval: none
 *******************************************************************************/
void bsp_gpio_out_set(GPIO_Type *port, uint8_t pin, uint8_t st)
{
	intx_alloc();

	intx_disable();

	cset_mask(port->GDIR, 1UL<<pin, (st?1UL:0UL)<<pin);

	intx_enable();
}

void bsp_gpio_mode_out_pp(GPIO_Type *port, uint8_t pin)
{
	intx_alloc();

	intx_disable();

	bit_set(port->GDIR, pin);

	intx_enable();
}

void bsp_gpio_mode_out_od(GPIO_Type *port, uint8_t pin)
{
	intx_alloc();

	intx_disable();

	bit_set(port->GDIR, pin);

	intx_enable();
}

void bsp_gpio_mode_out_pp_spd(GPIO_Type *port, uint8_t pin, uint32_t spd)
{
	intx_alloc();

	intx_disable();

	bit_set(port->GDIR, pin);
	
	intx_enable();
}

void bsp_gpio_mode_out_od_spd(GPIO_Type *port, uint8_t pin, uint32_t spd)
{
	intx_alloc();

	intx_disable();

	bit_set(port->GDIR, pin);

	intx_enable();
}

void bsp_gpio_mode_in_up(GPIO_Type *port, uint8_t pin)
{
	intx_alloc();

	intx_disable();

	bit_clr(port->GDIR, pin);
	
	intx_enable();
}

void bsp_gpio_mode_in_dn(GPIO_Type *port, uint8_t pin)
{
	intx_alloc();

	intx_disable();

	bit_clr(port->GDIR, pin);
	
	intx_enable();
}

void bsp_gpio_mode_in_ft(GPIO_Type *port, uint8_t pin)
{
	intx_alloc();

	intx_disable();

	bit_clr(port->GDIR, pin);

	intx_enable();
}

void bsp_gpio_mode_ain(GPIO_Type *port, uint8_t pin)
{
	intx_alloc();

	intx_disable();

	bit_clr(port->GDIR, pin);

	intx_enable();
}

void bsp_gpio_mode_set(GPIO_Type *port, uint8_t pin, char pm)
{
	switch (pm)
	{
	case PIN_MODE_IN_UP:
	{
		bsp_gpio_mode_in_up(port, pin);
		break;
	}
	case PIN_MODE_IN_DN:
	{
		bsp_gpio_mode_in_dn(port, pin);
		break;
	}
	case PIN_MODE_IN_FLOAT:
	{
		bsp_gpio_mode_in_ft(port, pin);
		break;
	}
	case PIN_MODE_IN_AIN:
	{
		bsp_gpio_mode_ain(port, pin);
		break;
	};
	case PIN_MODE_OUT_SL_PP:
	{
		bsp_gpio_mode_out_pp_spd(port, pin, 2);
		break;
	}
	case PIN_MODE_OUT_SL_OD:
	{
		bsp_gpio_mode_out_od_spd(port, pin, 2);
		break;
	}
	case PIN_MODE_OUT_SM_PP:
	{
		bsp_gpio_mode_out_pp_spd(port, pin, 2);
		break;
	}
	case PIN_MODE_OUT_SM_OD:
	{
		bsp_gpio_mode_out_od_spd(port, pin, 2);
		break;
	}
	case PIN_MODE_OUT_SH_PP:
	{
		bsp_gpio_mode_out_pp_spd(port, pin, 1);
		break;
	}
	case PIN_MODE_OUT_SH_OD:
	{
		bsp_gpio_mode_out_od_spd(port, pin, 1);
		break;
	}
	default:
	{
		break;
	}
	}
}

/*******************************************************************************
 * @brief  ext sim io
 *******************************************************************************/
uint8_t ext_io_out_get(char gp)
{
	uint8_t val = 0;

#define EXT_IO_OUT_BIT(n)    \
	if (ext_port_out[gp][n]) \
		val |= BIT(n);

	EXT_IO_OUT_BIT(0);
	EXT_IO_OUT_BIT(1);
	EXT_IO_OUT_BIT(2);
	EXT_IO_OUT_BIT(3);
	EXT_IO_OUT_BIT(4);
	EXT_IO_OUT_BIT(5);
	EXT_IO_OUT_BIT(6);
	EXT_IO_OUT_BIT(7);

	return val;
}
